#!/usr/bin/env sh
# Alias file that is Bash and Fish intercompatible

# TOOLS ENVIORNMENT SETTINGS
# Set Ansible config folder
export ANSIBLE_CONFIG="$HOME/.ansible/ansible.cfg"
# Use QEMU system URI
export LIBVIRT_DEFAULT_URI="qemu:///system"
# MCfly history settings
export MCFLY_RESULTS=30
export MCFLY_PROMPT="❯"
export MCFLY_FUZZY=5

# MY ALIASES BEGIN

# vi < vim < nvim substituton
export EDITOR='vi'
export VISUAL='vi'
command -v vim >/dev/null &&
	export EDITOR='vim' &&
	export VISUAL='vim' &&
	alias vi='vim' &&
	command -v nvim >/dev/null &&
	export EDITOR='nvim' &&
	export VISUAL='nvim' &&
	alias vim='nvim'

# FZF options for finding files
command -v fd >/dev/null &&
	export FZF_DEFAULT_COMMAND="fd --type f --strip-cwd-prefix --hidden --follow --exclude .git" ||
	export FZF_DEFAULT_COMMAND="find . --type f"

# Set preview aliases for Fuzzy Finder alias scripts
command -v eza >/dev/null &&
	# --color-scale only available with eza v >0.15.0
	export LS_PREVIEW_CMD="eza -al --git --color=always --color-scale --group-directories-first --icons --no-permissions " ||
	export LS_PREVIEW_CMD="ls -lahg --color=auto "
command -v bat >/dev/null &&
	export CAT_PREVIEW_CMD="bat --style header --style snip --style changes --style header " ||
	export CAT_PREVIEW_CMD="cat -n "

# Use z for zoxide alias
command -v zoxide >/dev/null &&
	alias z='zoxide' &&
	alias za='zoxide add' &&
	alias ze='zoxide edit' &&
	alias zi='zoxide import' &&
	alias zq='zoxide query' &&
	alias zr='zoxide remove'

## MY ALIASES
alias bashrc='source ~/.bashrc'
alias fishrc='source ~/.config/fish/config.fish'

# Common use
alias dus='du -hd 1'
alias duss='du -hd 1 | sort -hr'
alias grubup='sudo update-grub'
alias tarnow='tar -acf '
alias untar='tar -xvf '
alias wget='wget -c '
alias psmem='ps auxf | sort -nr -k 4'
alias psmem10='ps auxf | sort -nr -k 4 | head -10'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
# 'fgrep' is an obsolescent pattern for 'grep -F'
alias fgrep='grep -F --color=auto'
# 'egrep' is an obsolescent pattern for 'grep -E'
alias egrep='grep -E --color=auto'
alias hw='hwinfo --short' # Hardware Info
alias please='sudo'
alias tb='nc termbin.com 9999'
# Ip with colors
alias ip='ip -color'
# Get the error messages from journalctl
alias jctl="journalctl -p 3 -xb"

# Force tmux color
command -v tmux >/dev/null &&
	alias tmux='tmux -2 ' &&
	alias tmuxn='tmux -2 new -s ' &&
	# Use fuzzy finder to select what tmux session to attach to
	alias tmuxa='tmux attach -t $(tmux list-sessions | sed "s/:.*//" | fzf --header "Select what tmux session to attach to")'

# Fuzzy Finder alias scripts
# Fuzzy find and cd from current directory
alias f='cd $(find ./* -type d | fzf --header "Select what folder to cd into" --preview-window "up:50%" --preview-label "Folder contents" --preview "$LS_PREVIEW_CMD {1}")'
#alias f='cd $(find ./* -type d | fzf --header "Select what folder to cd into" --preview-window "up:50%" --preview-label "Folder contents" --preview "$LS_PREVIEW_CMD {1}")'
# Fuzzy find and cat/bat from current directory
alias fc='cat $(find ./* -type f | fzf --header "Select what file to cat" --preview-window "up:50%" --preview-label "File contents" --preview "$CAT_PREVIEW_CMD {1}")'
# Fuzzy find and vim edit from current directory
alias fv='vim $(find ./* -type f | fzf --header "Select what file to vi" --preview-window "up:50%" --preview-label "File contents" --preview "$CAT_PREVIEW_CMD {1}")'
# Fuzzy find and nvim edit from current directory
alias fnv='nvim $(find ./* -type f | fzf --header "Select what file to vi" --preview-window "up:50%" --preview-label "File contents" --preview "$CAT_PREVIEW_CMD {1}")'
# Fuzzy find dev directory type, then projects
alias dev='cd $(find ~/dev/* -maxdepth 1 -type d | fzf --header "Select what ~/dev folder to cd into" --preview-window "up:50%" --preview-label "Folder contents" --preview "$LS_PREVIEW_CMD {1}")'
# Fuzzy find dev directory all
alias deva='cd $(find ~/dev/* -type d | fzf --header "Select what ~/dev folder to cd into" --preview-window "up:50%" --preview-label "Folder contents" --preview "$LS_PREVIEW_CMD {1}")'
# Config folder fuzzy find
alias conf='cd $(find ~/.config/* -maxdepth 0 -type d | fzf --header "Select what ~/.config folder to cd into" --preview-window "up:50%" --preview-label "Folder contents" --preview-label "$LS_PREVIEW_CMD {1}")'
# Config folder fuzzy find all
alias confa='cd $(find ~/.config/* -type d | fzf --header "Select what ~/.config folder to cd into" --preview-window "up:50%" --preview-label "Folder contents" --preview "$LS_PREVIEW_CMD {1}")'
# Log tailer
alias logt='tail -f $(find /var/log -type f | fzf --header "Select what log file to tail" --preview-window "up:50%" --preview-label "File contents" --preview "tail -n 20 {1}")'
# Journalctl process log tailer
alias logj='ps aux | fzf | awk "{print $2}" | xargs -I % sudo sh -c journalctl _PID=%'
# Process killer
alias pkl='ps aux | fzf --header "Please select what process to kill" | awk "{print $2}" | xargs kill'

# Net aliases
alias curlpf="curl -o /dev/null -s -w 'HTTP %{http_code} - IP %{remote_ip} \n▲%{size_upload}B ▼%{size_download}B \n▲%{speed_upload}Bps ▼%{speed_download}Bps \nConnects %{num_connects} \nConnect %{time_connect}s \nLookup %{time_namelookup}s \nTransfer %{time_starttransfer}s (pre %{time_pretransfer}s) \nTotal %{time_total}s' "
# Pass gen
# alias pass-gen='< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c' # $1 - number of bytes

# Extract GPS data from image with imagemagick
# $1 file name
alias image-gps='identify -format "%[EXIF:GPSLatitude],%[EXIF:GPSLongitude]\n" '

# Sudo aliases
# List SSH sessions
alias lsssh="sudo netstat -tnpa | grep 'ESTABLISHED.*sshd'"
alias lsprt="sudo lsof -i -P -n"

# Python aliases
alias pip-install='pip install --upgrade --no-cache pip wheel setuptools '
alias python-venv='python3 -m venv venv && source ./venv/bin/activate'
alias dj-migrate='python manage.py makemigrations && python manage.py migrate'
alias pip-req-txt-to-toml="echo 'Adding requirements.txt to pyprpoject.toml' && cat requirements.txt | grep -E '^[^# ]' | cut -d= -f1 | xargs -n 1 poetry add"

# Rust aliases
alias clpy='cargo clippy -- -W clippy::pedantic -W clippy::nursery -W clippy::unwrap_used'
alias clpy-fix='cargo clippy --fix --allow-dirty -- -W clippy::pedantic -W clippy::nursery -W clippy::unwrap_used'

# Docker aliases
alias dk='docker'
alias dkup='sudo systemctl start docker.service'
alias dkdown='sudo systemctl stop docker.service'
alias dkstop='docker stop $(docker ps -a --format "{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.RunningFor}}" | fzf --header "Please select which container to stop" --preview "docker container inspect '{1}'" --preview-window "top:60%" --preview-label "Container inspect" | cut -f 1 -d " ")'
alias dkstopA='docker ps -a -q | xargs docker stop'
alias dkrm='docker rm $(docker ps -a --format "{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.RunningFor}}" | fzf --header "Please select which container to remove" --preview "docker container inspect '{1}'" --preview-window "top:60%" --preview-label "Container inspect" | cut -f 1 -d " ")'
alias dkrM='docker rm --force $(docker ps -a --format "{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.RunningFor}}" | fzf --header "Please select which container to remove" --preview "docker container inspect '{1}'" --preview-window "top:60%" --preview-label "Container inspect" | cut -f 1 -d " ")'
alias dkrmA='docker ps -a -q | xargs docker rm'
alias dkrmi='docker rmi $(docker images -a --format "{{.ID}}\t{{.Repository}}:{{.Tag}}\t{{.CreatedSince}}\t{{.Size}}" | fzf --header "Please select which image to remove" --preview "docker image inspect '{1}'" --preview-window "top:60%" --preview-label "Image inspect" | cut -f 1 -d " ")'
alias dkrmI='docker rmi --force $(docker images -a --format "{{.ID}}\t{{.Repository}}:{{.Tag}}\t{{.CreatedSince}}\t{{.Size}}" | fzf --header "Please select which image to remove" --preview "docker image inspect '{1}'" --preview-window "top:60%" --preview-label "Image inspect" | cut -f 1 -d " ")'
alias dkrmiA='docker images -a -q | xargs docker rmi'
alias dkvolrm='docker volume rm $(docker volume ls -q --format "{{.Name}}" | fzf --header "Please select which volume to remove" --preview "docker volume inspect '{1}'" --preview-window "top:60%" --preview-label "Volume inspect" | cut -f 1 -d " ")'
alias dkvolrmA='docker system prune --volumes --all --force && docker volume rm $(docker volume ls -q --format "{{.Name}}")'
alias dkprune='docker image prune && docker system prune -af'

# Mount rclone
alias rclone-crypt='rclone mount crypt:/ ~/Desktop/crypt/ --read-only --max-read-ahead=1G --acd-templink-threshold=0 --contimeout=15s --checkers=16 --bwlimit=0 --retries=3 --timeout=30s --low-level-retries=1 --transfers=8 --dir-cache-time=30m'
alias mi-display-fix='xrandr --output HDMI-0 --mode 3440x1440 --rate 60'

# Git aliases
alias g='git'
alias gst='git status'
alias gf='git diff'
alias gF='git diff --staged'
alias gl="git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(auto)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)'"
alias ga='git add '
alias gai='git add --interactive'
alias gA='git add -A'
alias gcm='git commit'
alias gcM='git commit --amend'
alias gp='git push'
alias gP='git push --force'
alias gsubu='git submodule update --recursive --remote'
# Use fuzzy finder to select a range from 1 to 20 to soft reset HEAD~N
alias gS="seq 1 20 | fzf --header 'HEAD~n number to soft reset' --preview 'git show --color HEAD~{1}' --preview-window 'right:80%' --preview-label 'Commit info' | xargs -I % sh -c 'git reset --soft HEAD~%'"
# Use fuzzy finder to select a range from 1 to 20 to hard reset HEAD~N
alias gH="seq 1 20 | fzf --header 'HEAD~n number to hard reset' --preview 'git show --color HEAD~{1}' --preview-window 'right:80%' --preview-label 'Commit info' | xargs -I % sh -c 'git reset --hard HEAD~%'"
# Use fuzzy finder to select a range from 1 to 20 to rebase HEAD~N
alias gR="seq 1 20 | fzf --header 'HEAD~n number to rebase' --preview 'git show --color HEAD~{1}' --preview-window 'right:80%' --preview-label 'Commit info' | xargs -I % sh -c 'git rebase -i HEAD~%'"
# Use fuzzy finder to select what git branch to checkout
alias gchk="git branch --format='%(refname:short)' | fzf --header 'Select git branch to checkout' --preview 'git show --color {1}' --preview-window 'right:80%' --preview-label 'Commit info' | xargs -I % sh -c 'git checkout %' "

## Useful aliases
# Replace ls with eza
# --color-scale only available with eza v >0.15.0
command -v eza >/dev/null &&
	# preferred listing
	alias ls='eza -al --git --color=always --color-scale --group-directories-first --icons' &&
	# long format
	alias ll='eza -alihgmuU --git --color=always --color-scale --group-directories-first --icons' &&
	# all files and dirs
	alias la='eza -a --color=always --color-scale --group-directories-first --icons' &&
	# tree listing
	alias lt='eza -aT --color=always --color-scale --group-directories-first --icons' &&
	# show only dotfiles
	alias l.='eza -a | grep -E "^\."' ||
	# Ls with with no exa
	alias ls='ls --color=auto -lhsi --author'

# Replace some more things with better alternatives
command -v pygmentize >/dev/null && alias cat='pygmentize -g '
command -v bat >/dev/null && alias cat='bat --style header --style snip --style changes --style header'
[ ! -x /usr/bin/yay ] && [ -x /usr/bin/paru ] && alias yay='paru'

# Bindings for Arch
command -v pacman >/dev/null &&
	alias rmpkg='sudo pacman -Rdd' &&
	# Sort installed packages according to size in MB
	alias big='expac -H M "%m\t%n" | sort -h | nl' &&
	# List amount of -git packages
	alias gitpkg='pacman -Q | grep -i "\-git" | wc -l' &&
	# Get fastest mirrors
	alias mirror="sudo reflector -f 30 -l 30 --number 10 --verbose --save /etc/pacman.d/mirrorlist" &&
	alias mirrord="sudo reflector --latest 50 --number 20 --sort delay --save /etc/pacman.d/mirrorlist" &&
	alias mirrors="sudo reflector --latest 50 --number 20 --sort score --save /etc/pacman.d/mirrorlist" &&
	alias mirrora="sudo reflector --latest 50 --number 20 --sort age --save /etc/pacman.d/mirrorlist" &&
	# Help people new to Arch
	alias apt='echo "Bruh, you are using Arch, btw" ' &&
	alias apt-get='echo "Bruh" ' &&
	# Cleanup orphaned packages
	alias cleanup='sudo pacman -Rns (pacman -Qtdq)' &&
	# Recent installed packages
	alias rip="expac --timefmt='%Y-%m-%d %T' '%l\t%n %v' | sort | tail -200 | nl"
